package terms;

import java.util.ArrayList;


public class LoopFinder {

	private ArrayList<ArrayList<Integer>> cycles;
	private AbstractGraph graph;
	private int to;

	public static void main(String[] args) {
		new LoopFinder().run();
	}

	private void run() {
		graph = new ArrayGraph(5);
		cycles = new ArrayList<ArrayList<Integer>>();
		graph.put(0, 2, new Gain(new DoubleResult(1)));
		graph.put(4, 0, new Gain(new DoubleResult(2)));
		graph.put(1, 2, new Gain(new DoubleResult(3)));
		graph.put(1, 3, new Gain(new DoubleResult(4)));
		graph.put(4, 1, new Gain(new DoubleResult(5)));
		graph.put(2, 3, new Gain(new DoubleResult(6)));
		graph.put(2, 4, new Gain(new DoubleResult(7)));
		graph.put(3, 4, new Gain(new DoubleResult(8)));
		findPathes(1, 4);
		/*graph = new ArrayGraph(5);
		cycles = new ArrayList<ArrayList<Integer>>();
		graph.put(0, 1, new Gain(new DoubleResult(1)));
		graph.put(1, 3, new Gain(new DoubleResult(2)));
		graph.put(1, 2, new Gain(new DoubleResult(3)));
		graph.put(2, 1, new Gain(new DoubleResult(4)));
		graph.put(2, 3, new Gain(new DoubleResult(5)));
		graph.put(3, 2, new Gain(new DoubleResult(6)));
		findLoops(graph);*/
// 		ArrayList<ArrayList<Integer>> l = 
//		System.out.println(l.toString());
	}

	public ArrayList<ArrayList<Integer>> findLoops(AbstractGraph graph) {
		cycles.clear();
		for (int i = 0, n = graph.size(); i < n; i++) {
			ArrayList<Integer> list = new ArrayList<Integer>();
			find(list, i);
			if (graph.get(i, i) != null) {
				list = new ArrayList<Integer>();
				list.add(i);
				cycles.add(list);
				System.out.println(list);
			}
		}
		System.out.println("cycles length = " + cycles.size());
		return cycles;
	}

	public ArrayList<ArrayList<Integer>> findPathes(int from, int to) {
		cycles.clear();
		this.to = to;
		ArrayList<Integer> list = new ArrayList<Integer>();
		findPath(list, from);
		return cycles;
	}

	@SuppressWarnings("unchecked")
	public void find(ArrayList<Integer> s, Integer now) {
		if (!s.isEmpty() && now.equals(s.get(0))) {
			s.add(s.get(0));
			if (! exist(s)) {
				cycles.add(s);
				System.out.println(s);
			}
			return;
		}
		if (s.contains(now))
			return;
		s.add(now);
		int[] indices = graph.execlusiveNext(now.intValue());
		if (indices.length == 1) {
			now = indices[0];
			find(s, now);
		} else if (indices.length == 0) // do nothing
			return;
		else { // >= 2
			for (int i = 0; i < indices.length; i++) {
				ArrayList<Integer> temp = (ArrayList<Integer>) s.clone();
				find(temp, indices[i]);
			}
		}
	}

	private boolean exist(ArrayList<Integer> elements) {
		boolean found = true;
		for (int i = 0, n = cycles.size(); i < n; i++) {
			ArrayList<Integer> temp = cycles.get(i);
			if (temp.size() == elements.size()) {
				found = true;
				for (int j = 0, k = temp.size(); j < k; j++)
					if (! temp.contains(elements.get(j)))
							found = false;
				if (found)
					return true;
			}
		}
		return false;
	}

	public ArrayList<ArrayList<Integer>> findPaths(int start, int end) {	
		return null;
	}

	@SuppressWarnings("unchecked")
	public void findPath(ArrayList<Integer> list, Integer now) {
		if (now.equals(to)) {
			list.add(to);
			cycles.add(list);
			System.out.println(list);
			return;
		}
		if (list.contains(now))
			return;
		list.add(now);
		int[] indices = graph.execlusiveNext(now.intValue());
		if (indices.length == 1) {
			now = indices[0];
			findPath(list, now);
		} else if (indices.length == 0) // do nothing
			return;
		else { // >= 2
			for (int i = 0; i < indices.length; i++) {
				ArrayList<Integer> temp = (ArrayList<Integer>) list.clone();
				findPath(temp, indices[i]);
			}
		}
	}

	public void setGraph(AbstractGraph graph) {
		this.graph = graph;
	}
}
